package com.zx.tradeclient.ctrl.index3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.tradeclient.R;
import com.zx.tradeclient.adapter.ProductPriceAdapter;
import com.zx.tradeclient.ctrl.index4.ProductInfoActivity;
import com.zx.tradeclient.entity.ProductPriceInfo;

public class ProductListActivity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private ListView product_price_list;
	private ProductPriceAdapter adapter;
	private ArrayList<ProductPriceInfo> list_info = new ArrayList<ProductPriceInfo>();

	private ProgressDialog progressDialog;
	private String mCompanyID = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_list);

		if (!(ActivityTaskManager.getInstance().containsName("ProductListActivity"))) {
			ActivityTaskManager.getInstance().putActivity("ProductListActivity", this);
		}

		init();

	}

	private void init() {
		mCompanyID = getIntent().getStringExtra("companyID");
		setHeader(0);
		adapter = new ProductPriceAdapter(ProductListActivity.this, list_info);

		product_price_list = (ListView) findViewById(R.id.product_list_price);

		product_price_list.setAdapter(adapter);
		product_price_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {

				Intent intent3 = new Intent(ProductListActivity.this, ProductInfoActivity.class);
				intent3.putExtra("productID", list_info.get(position).getProduct_id());

				startActivity(intent3);

			}
		});

		progressDialog = ProgressDialog.show(ProductListActivity.this, null, "正在获取数据，请稍后.....", true, true);
		getComProInfo();
	}

	private void getComProInfo() {
		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "productList");
		map.put("userid", mCompanyID);

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(ProductListActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

	private void parseJson(JSONObject response) {
		try {

			String succeed = response.getString("succeed");
			if (succeed.equals("000")) {
				String data = response.getString("dataList");

				JSONArray list = new JSONArray(data);

				for (int i = 0; i < list.length(); i++) {
					ProductPriceInfo info = new ProductPriceInfo();
					JSONObject log = list.getJSONObject(i);
					info.setProduct_id(log.getString("id"));
					info.setProduct_name(log.getString("name"));
					info.setProduct_price(log.getString("price"));

					list_info.add(info);
				}

			} else {
				Toast toast2 = Toast.makeText(ProductListActivity.this, "无数据", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			}

			progressDialog.dismiss();
			adapter.notifyDataSetChanged();

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {
			goback();
		} else if (v == header_right) {

		}

	}

}